package cn.edu.eight.food.service.Impl;

import cn.edu.eight.food.dao.OfOrderMapper;
import cn.edu.eight.food.dao.OrderMapper;
import cn.edu.eight.food.model.OfOrder;
import cn.edu.eight.food.model.OfUser;
import cn.edu.eight.food.model.Order;
import cn.edu.eight.food.service.ofOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ofOrderServiceImpl implements ofOrderService {
    @Autowired
    OfOrderMapper ofOrderMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public int insert(OfOrder record) {
        return ofOrderMapper.insert(record);
    }

    @Override
    public int insert(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public int buyFood(OfUser user, String[] ids,String[] nums) {
        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        OfOrder ofOrder=new OfOrder();
        Order order = new Order();
        for (int i=0;i<ids.length;i++){
            long id = d.getTime()+i;
            ofOrder.setOfOrderId((int)id);
            ofOrder.setOfOrderFoodId(Integer.parseInt(ids[i]));
            ofOrder.setOfOrderUserId(user.getOfUserId());
            ofOrder.setOfOrderNumber(Integer.parseInt(nums[i]));
            ofOrder.setOfOrderState(2);
            ofOrderMapper.insert(ofOrder);
            if (order.getOrderContent() == null){
                order.setOrderContent("#"+ofOrder.getOfOrderId());
            }else {
                order.setOrderContent(order.getOrderContent()+"#"+ofOrder.getOfOrderId());
            }
        }
        order.setOrderTime(sdf.format(d));
        orderMapper.insert(order);
        return 0;
    }

    @Override
    public OfOrder findById(String foodId,String userId) {
        return ofOrderMapper.findById(foodId,userId);
    }

    @Override
    public List<Map> orderList(String userId) {
        return ofOrderMapper.orderList(userId);
    }

    @Override
    public List<Map> orderListAll(String orderId, String userId,String mobile) {
        return ofOrderMapper.orderListAll(orderId,userId,mobile);
    }

    @Override
    public List<Map> cartList(String userId) {
        return ofOrderMapper.cartList(userId);
    }

    @Override
    public int saveCart(OfOrder ofOrder) {
        return ofOrderMapper.saveCart(ofOrder);
    }

    @Override
    public int delCart(String orderId) {
        return ofOrderMapper.delCart(orderId);
    }
}
